package main.java.com.dao.impl;

import main.java.com.config.BaseDao;
import main.java.com.dao.CategoryTwoDao;
import main.java.com.entity.Category;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CategoryTwoDaoImpl extends BaseDao implements CategoryTwoDao {

    /**
     * 展示数据库信息
     * @return 分类管理对象
     */
    @Override
    public List<Category> showinfo(int startIndex,int pageSize) {
        List<Category> list =new ArrayList<>();
        Category category = null;
        conn = this.getConnection();
        String sql = "SELECT * FROM `category` limit ?,?";
        try {
            rs = this.executeQuery(sql,startIndex,pageSize);
            while (rs.next()){
                category = new Category();
                category.setId(rs.getLong(1));
                category.setType(rs.getInt(2));
                category.setName(rs.getString(3));
                category.setSort(rs.getInt(4));
                category.setCreate_time(rs.getTime(5));
                category.setUpdate_time(rs.getTime(6));
                category.setCreate_user(rs.getLong(7));
                category.setUpdate_user(rs.getLong(8));
                list.add(category);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.closeAll(conn,psmt,rs);
        }
        return list;
    }

    @Override
    public int adddish(Category category) {
        int count = 0;
        conn = this.getConnection();
        String sql = "INSERT INTO `category`(`type`,`name`,`sort`,`create_time`,`update_time`,`create_user`,`update_user`) VALUES(1,?,?,now(),now(),1,1)";
        try {
            count=this.executeUpdate(sql,category.getName(),category.getSort());
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.closeAll(conn,psmt,rs);
        }
        return count;
    }

    @Override
    public int addpop(Category category) {
        int count = 0;
        conn = this.getConnection();
        String sql = "INSERT INTO `category`(`type`,`name`,`sort`,`create_time`,`update_time`,`create_user`,`update_user`) VALUES(2,?,?,now(),now(),1,1)";
        try {
            count=this.executeUpdate(sql,category.getName(),category.getSort());
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.closeAll(conn,psmt,rs);
        }
        return count;
    }

    @Override
    public int deletdish(Long id) {
        int count = 0;
        conn = this.getConnection();
        String sql = "DELETE FROM `category` WHERE id = ?";
        try {
            count = this.executeUpdate(sql,id);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.closeAll(conn,psmt,rs);
        }
        return count;
    }
    @Override
    public int updatedish(Category category) {
        int count =0;
        conn = this.getConnection();
        String sql = "UPDATE `category` SET `name`= ?,`sort`= ? WHERE `id` = ?";
        try {
            count = this.executeUpdate(sql,category.getName(),category.getSort(),category.getId());
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.closeAll(conn,psmt,rs);
        }
        return count;
    }


    @Override
    public int getPageCount() {
        int count =0;
        conn = this.getConnection();
        String sql = "SELECT COUNT(*) FROM `category`";
        try {
            rs = this.executeQuery(sql);
            while (rs.next()){
                count = rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.closeAll(conn,psmt,rs);
        }
        return count;
    }

}
